package test.gps;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import org.achartengine.util.MathHelper;

import com.baidu.platform.comapi.basestruct.GeoPoint;

import test.gps.GPSMapActivity.ProAT;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class SatelliteDataActivity extends Activity {
	public String mSatelliteData;
	public String mNum;
	public TextView mBack;
	public TextView mTitle;
	private GraphicalView mChartView; 
	private GraphicalView mChartView2;  
	private LinearLayout layout1;
	private LinearLayout layout2;
	HistoricalValuesChart mChart;
	ProbabilityChart mChart2;
	private ProgressBar mProgressBar;
	public TextView mProgressText;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_satellite_data);
		mBack = (TextView) findViewById(R.id.sateback);
		layout1 = (LinearLayout) findViewById(R.id.LinearLayout1);
		layout2 = (LinearLayout) findViewById(R.id.LinearLayout2);
		mTitle = (TextView) findViewById(R.id.satetitle);
		mProgressText = (TextView) findViewById(R.id.progressText2);
		mProgressBar = (ProgressBar)findViewById(R.id.progressBar2);  
		Intent intent1 = getIntent();
		mSatelliteData = intent1.getStringExtra("satedata");
		mNum=intent1.getStringExtra("satenum");
//		mTitle.setText(mSatelliteData);
		mTitle.setText("编号"+mNum+"卫星详细数据");
		mChart = new HistoricalValuesChart();
		mChart2 = new ProbabilityChart();
		ProAT pro = new ProAT();
		pro.execute();
		

      
		
		mBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

   	public class ProAT extends AsyncTask<String, Integer, String> {
		/*
		 * 第一个参数为doInBackground传入的参数，
		 * 第二个参数为onProgressUpdate传入参数,由doInBackground内调用publishProgress
		 * (Interger)传值; 第三个为doInBackground返回值和onPostExecute传入的参数。
		 */
		String text = "数据加载完成";
        String[] title = new String[] { "信噪比" };
       ArrayList<double[]> value = new ArrayList<double[]>();
       int[] color = new int[] { Color.YELLOW };
        PointStyle[] style = new PointStyle[] { PointStyle.CIRCLE };
        ArrayList<double[]> xt = new ArrayList<double[]>();
       
      
		
		
        String[] titles = new String[] { "信噪比概率分布" };
        List<double[]> x = new ArrayList<double[]>();
   
        List<double[]> values = new ArrayList<double[]>();
      
        int[] colors = new int[] { Color.YELLOW };
        PointStyle[] styles = new PointStyle[] { PointStyle.POINT };
		@Override
		protected void onPreExecute() {
			// 该方法将在执行实际的后台操作前被UI thread调用。可以在该方法中做一些准备工作，如在界面上转菊花提示
			System.out.println("onPreExecute--->Task start");
			super.onPreExecute();
			mProgressText.setText("数据加载中...");  
		}

		@Override
		protected String doInBackground(String... params) {
			// 将在onPreExecute 方法执行后马上执行，该方法运行在后台线程中。
			// 这里将主要负责执行那些很耗时的后台计算工作。
			System.out.println("doInBackground---->");
			String [] snrItem=mSatelliteData.split("#");
			 double[] v=new double[snrItem.length];
			 double[] xx=new double[snrItem.length];
			 int []c=new int [101];
			 double []p=new double [101];
			 double []db=new double [101];
			for(int i=0;i<snrItem.length;i++){
				String [] snrta=snrItem[i].split(",");
				v[i]=Double.parseDouble(snrta[5]);
				for(int j=0;j<101;j++){
					if((int)v[i]==j){
						c[j]++;
					}
				}
			}
			for(int j=0;j<101;j++){
					p[j]=(double)c[j]/(double)snrItem.length;
					p[j]=p[j]*100.0;
			}
			 value.add(v);
			 for(int i=0;i<snrItem.length;i++){
				
					xx[i]=i+1;
				}	  
			        
			 xt.add(xx);
			 for(int j=0;j<101;j++){
					db[j]=(double)j;
			}
			            x.add(db);
			        
			        values.add(p);    
	       
		    
	        
	        

     

			return "doInBackground";
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			System.out.println("更新进度" + values[0]);
			// 在publishProgress方法被调用后，UI
			// thread将调用这个方法从而在界面上展示任务的进展情况，例如通过一个进度条进行展示。
			super.onProgressUpdate(values);
		}

		@Override
		protected void onPostExecute(String result) {
			// 在doInBackground 执行完成后，onPostExecute 方法将被UI
			// thread调用，后台的计算结果将通过该方法传递到UI thread
			// 用于更新UI
			System.out.println("onPostExecute----->" + result);
			super.onPostExecute(result);
        	//执行地图刷新使生效
			mProgressText.setText(text);  

			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        mChartView2 = mChart2.execute(SatelliteDataActivity.this,title,xt, value, color,style ) ;
	        mChartView = mChart.execute(SatelliteDataActivity.this,titles,x, values, colors,styles ) ;

	        layout1.addView(mChartView2, new LayoutParams(LayoutParams.FILL_PARENT,
	        LayoutParams.FILL_PARENT));
	        layout2.addView(mChartView, new LayoutParams(LayoutParams.FILL_PARENT,
	                LayoutParams.FILL_PARENT));
			mProgressText.setVisibility(View.GONE);
			mProgressBar.setVisibility(View.GONE);

			
			
		}

		@Override
		protected void onCancelled() {
			// 当用户取消操作时.
			super.onCancelled();
		}
	}
}
